#include "TyrianEnvironment.h"
#include "../Entities/GameHUD.h"

TyrianEnvironment::TyrianEnvironment()
{
}

TyrianEnvironment::~TyrianEnvironment()
{
}

POINT3D TyrianEnvironment::gameCoord(POINT3D coord) {
	POINT3D gameCoord = {coord.x + 20, coord.y + 15, 0.0 };
	return  gameCoord;
}

POINT3D TyrianEnvironment::realCoord(POINT3D coord) {
	POINT3D realCoord = {coord.x * - 20, coord.y - 15, 0.0 };
	return  realCoord;
}

void TyrianEnvironment::collideWithShot(GameEntity * shot, POINT3D shotPosition) {
	SceneNode * rootSceneNode = SceneManager::Instance()->getRootSceneNode();
	GameEntity * entity;
	if(shot->getMySceneNode()->getParentSceneNode()->getEntity("reactiveenemy") != NULL ||
		shot->getMySceneNode()->getParentSceneNode()->getEntity("reactiveenemy2") != NULL) {
		entity = SceneManager::Instance()->getEntity("ship");
		if(collide(entity,shotPosition)) {
			shot->getMySceneNode()->destroy();
			GameEntity * entity2 = SceneManager::Instance()->getEntity("gameHUD");
			GameHUD * gamehud = (GameHUD*)entity2;
			gamehud->decHP(10);
		}
	}
	else {
		entity = SceneManager::Instance()->getEntity("reactiveenemy");
		if(entity != NULL) {
			if(collide(entity,shotPosition)) {
				POINT3D pos = entity->getPosition();
				int random = rand();
				if(random %2 == 0)
					pos.x = -20.0f;
				else pos.x = 15.0f;
				entity->setPosition(pos);
				MyGame::Instance()->incScore();
			}
		}
		entity = SceneManager::Instance()->getEntity("reactiveenemy2");
		if(entity != NULL) {
			if(collide(entity,shotPosition)) {
				POINT3D pos = entity->getPosition();
				int random = rand();
				if(random %2 == 0)
					pos.x = -20.0f;
				else pos.x = 15.0f;
				pos.y = 10.0f;
				entity->setPosition(pos);
				MyGame::Instance()->incScore();
			}
		}
	}
}

bool TyrianEnvironment::collide(GameEntity * entity, POINT3D shotPosition) {
	POINT3D pos = entity->getPosition();
	if( (shotPosition.x - 1) < pos.x && ( (shotPosition.x + 1) > pos.x) ) {
		if( (shotPosition.y - 1) < pos.y && ( (shotPosition.y + 1) > pos.y) ) {
			return true;
		}
		else return false;
	}
	else return false;
}



float TyrianEnvironment::distance(POINT3D p1, POINT3D p2) {
	return abs(p1.x - p2.x) + abs(p1.y - p2.y);					//Return manhattan distance
}

bool TyrianEnvironment::isLegalLocation(POINT3D p) {
	bool b;
	if(b = (p.x > -20 && p.x < 16 && p.y > -15 && p.y < 13)) 
		return true;
	
	return false;
}